#include "heap.h"
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>

#define container_of(ptr, type, member) ((type*) ((uintptr_t) (ptr) - offsetof(type, member)))

typedef struct heapdata_s {
    uint32_t    data;
    struct heap_node node;
}heapdata_t;

static int heapnode_compare(const struct heap_node* lhs, const struct heap_node* rhs) {
    return container_of(lhs, heapdata_t, node)->data < container_of(rhs, heapdata_t, node)->data;
}

int main(int argc, char const *argv[])
{
    struct heap heap_root;
    heap_init(&heap_root, heapnode_compare);

    heapdata_t h1 = { .data = 123 };
    heap_insert(&heap_root, &h1.node);

    heapdata_t h2 = { .data = 111 };
    heap_insert(&heap_root, &h2.node);

    heapdata_t h3 = { .data = 124 };
    heap_insert(&heap_root, &h3.node);

    heapdata_t h4 = { .data = 101 };
    heap_insert(&heap_root, &h4.node);

    heapdata_t h5 = { .data = 191 };
    heap_insert(&heap_root, &h5.node);

    heapdata_t h6 = { .data = 89 };
    heap_insert(&heap_root, &h6.node);

    heapdata_t h7 = { .data = 501 };
    heap_insert(&heap_root, &h7.node);

    heapdata_t* data_node = container_of(heap_root.root, heapdata_t, node);
    printf("count :%d, min :%d\n", heap_root.nelts, data_node->data);
    
    // 移除最小元素
    heap_dequeue(&heap_root);
    data_node = container_of(heap_root.root, heapdata_t, node);
    printf("count :%d, min :%d\n", heap_root.nelts, data_node->data);
    return 0;
}
